package com.example.peibei.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;

import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.LoginPasswordPresenter;
import com.example.peibei.service.presenter.UserSigPresenter;
import com.gyf.immersionbar.ImmersionBar;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.qcloud.tuicore.TUILogin;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginForPasswordActivity extends WDActivity {
    @BindView(R.id.et_phone)
    EditText phone;
    @BindView(R.id.et_sem)
    EditText sms;
    @BindView(R.id.checkbox)
    CheckBox checkbox;

    private LoginPasswordPresenter loginPasswordPresenter;
    private UserSigPresenter userSigPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_password;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this)
                .fitsSystemWindows(false)
                .fullScreen(true)
                .init();
        loginPasswordPresenter = new LoginPasswordPresenter(new LoginCall());
        userSigPresenter = new UserSigPresenter(new UserSigCall());
    }

    @OnClick(R.id.bt_login)
    public void login() {
        String phoneNum = phone.getText().toString();
        String smsNum = sms.getText().toString();
        if (!checkbox.isChecked()){
            hideInput();
            UIUtils.showToastSafe("登录前，请先同意用户协议和隐私政策");
            return;
        }
        if (TextUtils.isEmpty(phoneNum)) {
            UIUtils.showToastSafe("请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(smsNum)) {
            UIUtils.showToastSafe("密码不能为空");
            return;
        }
        loginPasswordPresenter.reqeust(phoneNum, smsNum);
//        if (TextUtils.isEmpty(tuiguang.getText())) {
//            loginPasswordPresenter.reqeust(phoneNum, smsNum, "");
//        } else {
//            loginPasswordPresenter.reqeust(phoneNum, smsNum, tuiguang.getText().toString());
//        }
    }

    /**
     * 隐藏键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    class LoginCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("登录成功");
            if (!TextUtils.isEmpty(data)) {
                SPUtils spUtils = new SPUtils(LoginForPasswordActivity.this, SpConstant.USER_TOKEN);
                spUtils.putString(SpConstant.USER_TOKEN, data);
                userSigPresenter.reqeust( "Bearer " + data);
            }
//            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    class UserSigCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            SPUtils spUtils = new SPUtils(LoginForPasswordActivity.this, SpConstant.USER_TOKEN);
            spUtils.putString(SpConstant.USER_SIG,data);
            intent(MainActivity.class);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @OnClick(R.id.tv_login_ver_code)
    public void toLoginCode() {
        intent(LoginActivity.class);
        finish();
    }

    @OnClick(R.id.iv_close_login)
    public void close() {
        finish();
    }

    @OnClick(R.id.tv_user_Agree)
    public void userAgree() {
        Bundle bundle = new Bundle();
        bundle.putString("type","userAgree");
        intent(UserAgreeActivity.class,bundle);
    }

    @OnClick(R.id.tv_private_agree)
    public void privateAgree() {
        Bundle bundle = new Bundle();
        bundle.putString("type","privateAgree");
        intent(UserAgreeActivity.class,bundle);
    }

    private IWXAPI api;
    private static final String APP_ID = "wx4de7acf350b321e9";

    @OnClick(R.id.ll_login_wx)
    public void loginWx() {
        if (!checkbox.isChecked()) {
            hideInput();
            UIUtils.showToastSafe("登录前，请先同意用户协议和隐私政策");
            return;
        }
//         通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, true);
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo_test";
        api.sendReq(req);
//        getSign();
    }

    @Override
    protected void destoryData() {

    }
}
